package com.hang.gymmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hang.gymmanager.Enum.CommonConstant;
import com.hang.gymmanager.Enum.CommonEnum;
import com.hang.gymmanager.Enum.IndexPlaceStatusEnum;
import com.hang.gymmanager.Enum.PlaceStatusEnum;
import com.hang.gymmanager.common.Response;
import com.hang.gymmanager.entity.*;
import com.hang.gymmanager.res.IndexPlaceStatusRes;
import com.hang.gymmanager.res.IndexRes;
import com.hang.gymmanager.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页控制器
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/3/22 14:40
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("")
@CrossOrigin
public class IndexController {

    private final CommodityService commodityService;
    private final UserInfoService userInfoService;
    private final PlaceService placeService;

    private final OrderService orderService;

    private final SeatService seatService;


    /**
     * 首页数据
     * @return
     */
    @GetMapping("/index")
    public Response<IndexRes> indexData(){
        //系统介绍
        IndexRes res = new IndexRes();
        res.setSystemName(CommonConstant.SYSTEM_NAME);
        res.setIntroduce(CommonConstant.SYSTEM_INTRODUCE);
        //用户总人数
        QueryWrapper<UserInfoEntity> userWrapper = new QueryWrapper<>();
        userWrapper.lambda().eq(UserInfoEntity::getRecFlag, CommonEnum.YES.getValue());
        Long userCount = userInfoService.getBaseMapper().selectCount(userWrapper);
        res.setUserCounter(userCount);
        //获取当天订单总金额
        QueryWrapper<OrderEntity> orderWrapper = new QueryWrapper<>();
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endOfDay = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).plusSeconds(-1);
        orderWrapper.lambda().gt(OrderEntity::getFinishTime, startOfDay)
                .lt(OrderEntity::getFinishTime,endOfDay);
        List<OrderEntity> orderEntities = orderService.getBaseMapper().selectList(orderWrapper);
        if(orderEntities.size()==0){
            res.setSale(new BigDecimal(0));
        }else {
            BigDecimal total = new BigDecimal(0);
            for (OrderEntity e:orderEntities){
                total =total.add(e.getPrice());
            }
            res.setSale(total);
        }
        //库存预警
        QueryWrapper<CommodityEntity> commodityWrapper = new QueryWrapper<>();
        commodityWrapper.lambda().lt(CommodityEntity::getInventory,CommonConstant.WARN_COUNT);
        Long warnCount = commodityService.getBaseMapper().selectCount(commodityWrapper);
        res.setWarnCommodity(warnCount);
        //场地状态
        QueryWrapper<PlaceEntity> placeWrapper = new QueryWrapper<>();
        placeWrapper.lambda().orderByAsc(PlaceEntity::getPlaceId);
        List<PlaceEntity> placeEntities = placeService.getBaseMapper().selectList(placeWrapper);
        List<IndexPlaceStatusRes> result = new ArrayList<>();
        placeEntities.stream().forEach(e->{
            IndexPlaceStatusRes placeStatus = new IndexPlaceStatusRes();
            placeStatus.setPlaceId(e.getPlaceId());
            if(e.getPlaceStatus() == PlaceStatusEnum.BUSY.getValue()){
                placeStatus.setPlaceStatus(IndexPlaceStatusEnum.BUSY.getValue());
            }else {
                if(LocalDateTime.now().plusHours(1).isBefore(e.getNextTime())){
                    placeStatus.setPlaceStatus(IndexPlaceStatusEnum.LEISURE.getValue());
                }else {
                    placeStatus.setPlaceStatus(IndexPlaceStatusEnum.PRE_BUSY.getValue());
                }
            }
            result.add(placeStatus);
        });
        res.setList(result);
        return Response.success(res);
    }


}
